package com.bocai.kelala.ui.travel.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.AddressListBean;
import com.bocai.kelala.bean.MyInvoiceBean;
import com.bocai.kelala.bean.MyInvoiceDetailBean;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.set.contract.MyInvoiceContract;
import com.bocai.kelala.ui.travel.set.module.MyInvoiceModule;
import com.bocai.kelala.ui.travel.set.presenter.MyInvoicePresenter;
import com.bocai.kelala.util.SP;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/12.
 */

public class MyAddInvoice extends BaseActivity implements MyInvoiceContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
//    @BindView(R.id.check_personal)
//    CheckBox checkPersonal;
    @BindView(R.id.ll_personal)
    LinearLayout llPersonal;
//    @BindView(R.id.check_company)
//    CheckBox checkCompany;
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.et_invitation_code)
    EditText etInvitationCode;
    @BindView(R.id.ll_add_ship_address)
    LinearLayout llAddShipAddress;
    @BindView(R.id.tv_receipt_name)
    TextView tvReceiptName;
    @BindView(R.id.tv_receipt_phone)
    TextView tvReceiptPhone;
    @BindView(R.id.tv_receipt_address)
    TextView tvReceiptAddress;
    @BindView(R.id.ll_ship_address)
    LinearLayout llShipAddress;
    @BindView(R.id.ll_head)
    LinearLayout llHead;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.et_shui_code)
    EditText etShuiCode;
    private String orderId;
    @Inject
    MyInvoicePresenter presenter;
    AddressListBean.AddressesBean bean = new AddressListBean.AddressesBean();
    private int flag = 1;

    @Override
    public int getLayoutId() {
        return R.layout.act_my_add_invoice;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MyInvoiceModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("我的发票");
        initBack(imgBack, llBack);
        orderId = getIntent().getStringExtra("orderId");
        if (TextUtils.isEmpty(SP.get(this, "addressId", "").toString())) {
            llAddShipAddress.setVisibility(View.VISIBLE);
            llShipAddress.setVisibility(View.GONE);
        } else {
            llAddShipAddress.setVisibility(View.GONE);
            llShipAddress.setVisibility(View.VISIBLE);
            tvReceiptName.setText(SP.get(this, "name", "").toString());
            tvReceiptPhone.setText(SP.get(this, "phone", "").toString());
            tvReceiptAddress.setText(SP.get(this, "regionName", "").toString() + SP.get(this, "address", "").toString());
        }

    }

    @OnClick({R.id.ll_personal, R.id.ll_company, R.id.ll_add_ship_address, R.id.ll_ship_address, R.id.btn})
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.ll_personal:
//                checkPersonal.setChecked(true);
//                checkCompany.setChecked(false);
//                llHead.setVisibility(View.GONE);
//                flag = 0;
//                break;
//            case R.id.ll_company:
//                checkPersonal.setChecked(false);
//                checkCompany.setChecked(true);
//                llHead.setVisibility(View.VISIBLE);
//                flag = 1;
//                break;
            case R.id.ll_add_ship_address:
                startActivityForResult(new Intent(MyAddInvoice.this, AddressActivity.class).putExtra("index", 1), 333);
//                startActivity(AddressActivity.class);
                break;
            case R.id.ll_ship_address:
                startActivityForResult(new Intent(MyAddInvoice.this, AddressActivity.class).putExtra("index", 1), 333);
                break;
            case R.id.btn:
                if (TextUtils.isEmpty(etInvitationCode.getText().toString())) {
                    showErrorTip("请输入您的发票抬头");
                } else if ( TextUtils.isEmpty(etShuiCode.getText().toString())) {
                    showErrorTip("请输入税号");
                } else if ((bean == null || bean.getId() == null) && TextUtils.isEmpty(SP.get(this, "addressId", "").toString())) {
                    showErrorTip("请添加收件地址");
                } else {
                    if (bean == null || bean.getId() == null) {
                        presenter.openInvoice(orderId, etShuiCode.getText().toString(), etInvitationCode.getText().toString(), SP.get(this, "addressId", "").toString());
                    } else {
                        presenter.openInvoice(orderId, etShuiCode.getText().toString(), etInvitationCode.getText().toString(), bean.getId());
                    }
                }
                break;

        }
    }

    @Override
    public void getMyInvoiceList(MyInvoiceBean bean) {

    }

    @Override
    public void openInvoiceSuccess() {
        showErrorTip("开票请求已提交，请耐心等待");
        setResult(444);
        finish();
    }

    @Override
    public void getInvoiceDetail(MyInvoiceDetailBean bean) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {

        if (msg.equals("用户授权失败")) {
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this, MainActivity.class).putExtra("index", 1));
            finish();
        }
        showShortToast(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 333 && resultCode == 333 && data != null) {
            this.bean = (AddressListBean.AddressesBean) data.getSerializableExtra("data");
            llAddShipAddress.setVisibility(View.GONE);
            llShipAddress.setVisibility(View.VISIBLE);
            tvReceiptName.setText(bean.getContacts());
            tvReceiptPhone.setText(bean.getPhone());
            tvReceiptAddress.setText(bean.getRegionName() + bean.getAddress());
        }
    }
}
